﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.SessionState;
using System.Drawing;
using System.IO;
using Common;
using Model;
using DAL;
using System.Drawing.Imaging;

namespace UI.tools
{
    /// <summary>
    /// Upload 的摘要说明
    /// </summary>
    public class Upload : IHttpHandler, IRequiresSessionState
    {
        HttpContext content;
        public void ProcessRequest(HttpContext context)
        {
            content = context;
            string handle = context.Request["handle"];
            context.Response.ContentType = "text/plain";
            if (handle == "upload_file_o") //上传文件，不生成缩略图
            {
                string path = UploadFile(false);
                context.Response.Write(path);
            }
            if (handle == "upload_file_p") //上传产品图片，不生成缩略图
            {
                string path = UploadFile(false);
                context.Response.Write(path);
            }
            if (handle == "upload_file_t") //上传文件，生成带缩略图
            {
                string path = UploadFile(true);
                context.Response.Write(path);
            }
            if (handle == "upload_file_a") //上传文件，生成带缩略图
            {
                int sid = JobRequest.GetRequestInt("sid");
                int userid = JobRequest.GetRequestInt("userid");
                string tablename = JobRequest.GetRequestString("tablename");
                UploadAnnex(sid, userid, tablename);
                context.Response.Write("");
            }
        }
        
        /// <summary>
        /// 
        /// </summary>
        /// <param name="thumb">是否生成缩略图</param>
        /// <returns></returns>
        public string UploadFile(bool thumb)
        {
            string opath = "/upload/" + DateTime.Now.ToString("yyyy-MM-dd") + "/";
            var rootpath = content.Server.MapPath(opath);
            if (!System.IO.Directory.Exists(rootpath))
            {
                Directory.CreateDirectory(rootpath);
            }
            string path = "";
            for (int j = 0; j < content.Request.Files.Count; j++)
            {
                HttpPostedFile uploadFile = content.Request.Files[j];
                if (uploadFile.ContentLength > 0)
                {
                    string ext = Path.GetExtension(uploadFile.FileName);
                    string unique = DateTime.Now.ToString("HHmmssfff");
                    string file = rootpath + unique + ext;
                    string name = file.Substring(0, file.LastIndexOf('.'));
                    uploadFile.SaveAs(file);
                    if (thumb)
                    {
                        string imgpath200 = name + "_200" + ext;
                        string imgpath500 = name + "_500" + ext;
                        string imgpath1000 = name + "_1000" + ext;
                        Thumbnail.MakeThumbnailImage(file, imgpath200, 200, 200);
                        Thumbnail.MakeThumbnailImage(file, imgpath500, 500, 500);
                        Thumbnail.MakeThumbnailImage(file, imgpath1000, 1000, 1000);
                    }
                    path = opath + unique + ext + "*" + uploadFile.FileName;
                }
            }
            return path;
        }
        public static void UploadAnnex(int sid, int userid, string tablename)
        {
            var content = HttpContext.Current;
            string opath = "/upload/" + DateTime.Now.ToString("yyyy-MM-dd") + "/";
            var rootpath = content.Server.MapPath(opath);
            if (!System.IO.Directory.Exists(rootpath))
            {
                Directory.CreateDirectory(rootpath);
            }
            string[] keys = content.Request.Files.AllKeys;
            for (int j = 0; j < content.Request.Files.Count; j++)
            {
                HttpPostedFile uploadFile = content.Request.Files[j];
                if (uploadFile.ContentLength > 0 && keys[j] == "img")
                {
                    string ext = Path.GetExtension(uploadFile.FileName);
                    string unique = DateTime.Now.ToString("HHmmssfff");
                    string file = rootpath + unique + ext;
                    string name = file.Substring(0, file.LastIndexOf('.'));
                    uploadFile.SaveAs(file);
                    string imgpath200 = name + "_200" + ext;
                    string imgpath500 = name + "_500" + ext;
                    Thumbnail.MakeThumbnailImage(file, imgpath200, 200, 200);
                    Thumbnail.MakeThumbnailImage(file, imgpath500, 500, 500);

                    AnnexInfo info = new AnnexInfo()
                    {
                        SID = sid,
                        UserID = userid,
                        Name = uploadFile.FileName,
                        Path = opath + unique + ext,
                        Size = uploadFile.ContentLength,
                        TableName = tablename,
                        Type = 1,
                        CreateDate = DateTime.Now
                    };
                    AnnexDAL.Save(info);
                }
            }
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}